package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * TODO
 *
 * @author Zhy
 * @version 1.0
 * @date 2025-04-01 16:35
 */
@Component
@Slf4j
public class CustomerClient {

	@Resource
	private AddressBookApi addressBookApi;

	/**
	 * 根据 ID 查询地址簿信息
	 *
	 * @return:
	 * @param:
	 */
	@SentinelResource(value = "findAddressBookDetail", fallback = "findAddressBookDetailFallback", blockHandler = "findAddressBookDetailBlockHandler")
	public AddressBookResDTO findAddressBookDetail(
		Long addressBookId
	) {
		return addressBookApi.detail(addressBookId);
	}

	/**
	 * 降级方法
	 *
	 * @return:
	 * @param:
	 */
	public AddressBookResDTO findAddressBookDetailFallback(Long addressBookId, Throwable t) {
		log.error("根据 ID 查询地址簿信息失败，服务降级，返回默认值，错误信息：{}", t.getMessage());
		AddressBookResDTO dto = new AddressBookResDTO();
		dto.setId(-1L);
		return dto;
	}

	/**
	 * 熔断方法
	 *
	 * @return:
	 * @param:
	 */
	public AddressBookResDTO findAddressBookDetailBlockHandler(Long addressBookId, BlockException be) {
		log.error("根据 ID 查询地址簿信息失败，服务熔断，返回默认值，错误信息：{}", be.getMessage());
		AddressBookResDTO dto = new AddressBookResDTO();
		dto.setId(-2L);
		return dto;
	}

}
